package xy.donate.android.ui.activity

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import cn.jzvd.JzvdStd
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder

import com.hjq.http.EasyHttp
import com.hjq.http.listener.HttpCallback
import com.hjq.http.listener.OnHttpListener
import kotlinx.android.synthetic.main.activity_open_eye_detail.*
import xy.donate.android.R
import xy.donate.android.common.MyActivity
import xy.donate.android.helper.TimeUtils
import xy.donate.android.http.model.HttpData
import xy.donate.android.http.request.OeRelateVideoApi
import xy.donate.android.http.request.VideoCommentApi
import xy.donate.android.openeye.*
import xy.donate.android.openeye.Item.Companion.TYPE_ONE
import xy.donate.android.openeye.Item.Companion.TYPE_THREE
import xy.donate.android.openeye.Item.Companion.TYPE_TWO
import java.lang.Exception

/**
 * @author XY
 */
class OpenEyeDetailActivity : MyActivity() {
    var list = mutableListOf<OpenEyeDetailBean>()
    private lateinit var myAdapter: MyAdapter
    private lateinit var playUrl: String
    private lateinit var title: String
    private lateinit var des: String
    private lateinit var id: String
    private lateinit var coverImage: String
    private lateinit var tag: String
    lateinit var jzVideoPlayerStandard: JzvdStd

    override fun getLayoutId(): Int {
        return R.layout.activity_open_eye_detail
    }

    override fun initView() {
        jzVideoPlayerStandard = findViewById(R.id.videoplayer)
        getIntentParms()
        rvRelatedRideo.layoutManager =
                LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        myAdapter = MyAdapter(list)
        rvRelatedRideo.adapter = myAdapter
        myAdapter.setHeaderView(getHeadView(), 0)
        myAdapter.setOnItemClickListener { adapter, view, position ->
            val item = adapter.data[position] as OpenEyeDetailBean
            if (item.itemType == TYPE_ONE) {
                val intent = Intent(this, OpenEyeDetailActivity::class.java)
                val openEyeCusBean = OpenEyeCusBean(
                        item.video.title,
                        item.video.description,
                        item.video.playUrl,
                        "" + item.video.id,
                        "" + item.video.coverImage,
                        item.video.category
                )
                intent.putExtra("openEyeCusBean", openEyeCusBean)
                startActivity(intent)
                finish()
            }
        }

        iv_back.setOnClickListener { finish() }
    }


    private fun getIntentParms() {
        val openEyeCusBean = intent.getParcelableExtra<OpenEyeCusBean>("openEyeCusBean")
        if (openEyeCusBean != null){
            playUrl = openEyeCusBean.playUrl
            title = openEyeCusBean.title
            des = openEyeCusBean.des
            id = openEyeCusBean.id
            coverImage = openEyeCusBean.coverImage
            tag = openEyeCusBean.tag
            loadData()
        }
    }

    private fun loadData() {
        jzVideoPlayerStandard.setUp(playUrl, title)
        Glide.with(this)
                .load(coverImage)
                .placeholder(R.drawable.common_shape_image_default_bg)
                .error(R.drawable.common_shape_image_default_bg)
                .into(jzVideoPlayerStandard.posterImageView)
        EasyHttp.get(this)
                .api(OeRelateVideoApi().setId(id))
                .request(object : OnHttpListener<OpenEyeRelateVideoBean> {
                    override fun onSucceed(result: OpenEyeRelateVideoBean?) {
                        getOeRelateVideoShow(result!!)
                    }

                    override fun onFail(e: Exception?) {
                        hideDialog()
                        toast(e?.message)
                    }
                })
    }

    private fun getHeadView(): View {
        var view = LayoutInflater.from(this).inflate(R.layout.activity_open_eye_detail_header, null)
        view.findViewById<TextView>(R.id.oeDetailTitle).text = title
        view.findViewById<TextView>(R.id.oeDetailCategory).text = "#${tag}"
        view.findViewById<TextView>(R.id.oeDetailDes).text = des
        return view
    }

    override fun initData() {
    }

    override fun onBackPressed() {
        if (JzvdStd.backPress()) {
            return
        }
        super.onBackPressed()
    }

    override fun onPause() {
        super.onPause()
        JzvdStd.releaseAllVideos()
    }


    class MyAdapter(data: MutableList<OpenEyeDetailBean>?) :
            BaseMultiItemQuickAdapter<OpenEyeDetailBean, BaseViewHolder>(data), LoadMoreModule {
        init {
            addItemType(TYPE_ONE, R.layout.item_activity_open_eye_detail_related_rideo)
            addItemType(TYPE_TWO, R.layout.item_activity_open_eye_detail_related_comment)
            addItemType(TYPE_THREE, R.layout.item_activity_open_eye_detail_related_comment_des)
        }

        override fun convert(holder: BaseViewHolder, item: OpenEyeDetailBean) {
            when (item?.itemType) {
                TYPE_ONE -> {
                    var ivOeDetailCoverImage = holder.getView<ImageView>(R.id.ivOeDetailCoverImage)
                    var tvOeDetailDuration = holder.getView<TextView>(R.id.tvOeDetailDuration)
                    var tvOeDetailTitle = holder.getView<TextView>(R.id.tvOeDetailTitle)
                    var tvOeDetailTag = holder.getView<TextView>(R.id.tvOeDetailTag)

                    Glide.with(ivOeDetailCoverImage)
                            .load(item.video.coverImage)
                            .apply(RequestOptions.bitmapTransform(RoundedCorners(10)))
                            .into(ivOeDetailCoverImage)
                    tvOeDetailDuration.text = TimeUtils.convertMinAndSec(item.video.duration)
                    tvOeDetailTitle.text = item.video.title
                    tvOeDetailTag.text = " #${item.video.category}"
                }
                TYPE_TWO -> {
                    var ivOeDetailUserLogo = holder.getView<ImageView>(R.id.ivOeDetailUserLogo)
                    var tvOeDetailUserName = holder.getView<TextView>(R.id.tvOeDetailUserName)
                    var tvOeDetailUserComment = holder.getView<TextView>(R.id.tvOeDetailUserComment)
                    var tvOeDetailUserCommentTime =
                            holder.getView<TextView>(R.id.tvOeDetailUserCommentTime)

                    Glide.with(ivOeDetailUserLogo)
                            .load(item.comment.avatar)
                            .placeholder(R.drawable.default_openeye_user)
                            .error(R.drawable.default_openeye_user)
                            .into(ivOeDetailUserLogo)
                    tvOeDetailUserName.text = item.comment.nickname
                    tvOeDetailUserComment.text = item.comment.message
                    tvOeDetailUserCommentTime.text =
                            TimeUtils.formatDateLongToString(item.comment.createTime, "HH:mm")
                }
                TYPE_THREE -> {
                    var tvOeDetailCommentDes = holder.getView<TextView>(R.id.tvOeDetailCommentDes)
                    tvOeDetailCommentDes.text = item.commentDes
                }
                else -> {

                }
            }
        }
    }

    fun getOeRelateVideoShow(data: OpenEyeRelateVideoBean) {
        if (data.itemList.isNotEmpty()) {
            hideDialog()
            for (item in data.itemList) {
                if (item.type == "videoSmallCard") {
                    var videoData = item.data
                    var video = Video(
                            videoData.id,
                            videoData.description,
                            videoData.title,
                            videoData.category,
                            videoData.cover.feed,
                            videoData.duration,
                            videoData.playUrl
                    )
                    var openEyeDetailBean = OpenEyeDetailBean(TYPE_ONE)
                    openEyeDetailBean.video = video
                    list.add(openEyeDetailBean)
                }
            }
            hideDialog()
        } else {
            hideDialog()
        }
        EasyHttp.get(this)
                .api(VideoCommentApi().setVideoId(id))
                .request(object : OnHttpListener<OpenEyeRelateCommentBean> {
                    override fun onSucceed(result: OpenEyeRelateCommentBean?) {
                        getOeRelateCommentShow(result!!)
                    }

                    override fun onFail(e: Exception?) {
                        hideDialog()
                        toast(e?.message)
                    }
                })
    }

    fun getOeRelateCommentShow(data: OpenEyeRelateCommentBean) {
        if (data.itemList.isNotEmpty()) {
            var openEyeDetailBean = OpenEyeDetailBean(TYPE_THREE)
            openEyeDetailBean.commentDes = "最新评论"
            list.add(openEyeDetailBean)
            for (item in data.itemList) {
                if (item.data.dataType == "ReplyBeanForClient") {
                    var commentItem = item.data
                    var commentBean = Comment(
                            commentItem.user.nickname,
                            commentItem.user.avatar,
                            commentItem.message,
                            commentItem.createTime
                    )
                    var openEyeDetailBean = OpenEyeDetailBean(TYPE_TWO)
                    openEyeDetailBean.comment = commentBean
                    list.add(openEyeDetailBean)
                }
            }
            hideDialog()
        } else {
            hideDialog()
        }

        myAdapter.setList(list)
    }
}